//
//  CustomTextMessageTableViewCell.m
//  WeWin
//
//  Created by lifayu on 2018/9/26.
//  Copyright © 2018年 lifayu. All rights reserved.
//

#import "CustomTextMessageTableViewCell.h"

@implementation CustomTextMessageTableViewCell

- (UIView *)bubbleView{
    if(!_bubbleView){
        _bubbleView = [[UIView alloc] init];
    }
    return _bubbleView;
}

- (UIImageView *)bubbleBgView{
    if(!_bubbleBgView){
        _bubbleBgView = [[UIImageView alloc] init];
        _bubbleBgView.userInteractionEnabled = YES;
        FlyWeakSelf
        [_bubbleView bk_whenTapped:^{
        }];
    }
    return _bubbleBgView;
}

- (UILabel *)messageLabel{
    if(!_messageLabel){
        _messageLabel = [[UILabel alloc] init];
        _messageLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
        _messageLabel.numberOfLines = 0;
    }
    return _messageLabel;
}

- (void)addOwnViews{
    [super addOwnViews];
    [self.contentView addSubview:self.bubbleView];
    [self.bubbleView addSubview:self.bubbleBgView];
    [self.bubbleView addSubview:self.messageLabel];
}


- (void)configOwnViews{
    [super configOwnViews];
    
    _messageLabel.attributedText = [[NSAttributedString alloc] initWithString:self.messageModel.content attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14]}];
    if([self.messageModel.adminId isEqualToString:@"user"]){
        _messageLabel.textColor = kWhiteColor;
        self.bubbleBgView.image = [[UIImage imageNamed:@"sender_bg"] stretchableImageWithLeftCapWidth:50 topCapHeight:38];
    }else{
        _messageLabel.textColor = UIColorFromRGB(0x36455D);
        self.bubbleBgView.image = [[UIImage imageNamed:@"receiver_bg"] stretchableImageWithLeftCapWidth:50 topCapHeight:38];
    }
}

- (void)layoutSubviews{
    [super layoutSubviews];

    NSAttributedString *text =  [[NSAttributedString alloc] initWithString:self.messageModel.content attributes:@{NSFontAttributeName:[UIFont fontWithName:@"PingFangSC-Regular" size:14]}];
    CGRect rect = [text boundingRectWithSize:CGSizeMake(MaxBubbleViewWidth - 16*2, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading context:nil];
    CGSize textSize = rect.size;
    
    self.messageLabel.size = textSize;
    self.bubbleView.size = CGSizeMake(textSize.width + 32, textSize.height + 20);
    
    
    if([self.messageModel.poster isEqualToString:@"user"]){
        self.bubbleView.top = MessageCellPadding;
        self.bubbleView.right = self.portraitImageView.left - 8;
    }else{
        self.bubbleView.top = MessageCellPadding + 20;
        self.bubbleView.left = self.portraitImageView.right + 8;
    }
    self.bubbleBgView.frame = self.bubbleView.bounds;
    self.messageLabel.top = 10;
    self.messageLabel.left = 16;
}


@end
